package com.codefish.framework.spring.core.utils;

import com.sun.istack.internal.NotNull;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
 * IO操作相关工具类
 *
 * @author <a href='https://gitee.com/hzf2281'>codefish</a>
 * @version 1.0.0
 * @since 2022/09/30 下午 05:32
 */
public class IoUtils {
    private static final int MAX_BUFFER_SIZE = 4 * 1024 * 1024;

    public static String getUtf8String(@NotNull InputStream is) throws IOException {
        AssertUtils.notNull(is,"输入流不能为null");
        byte[] buf = new byte[MAX_BUFFER_SIZE];
        int len = is.read(buf);
        if (len == MAX_BUFFER_SIZE) {
            throw new IllegalArgumentException("字符串占用大小不能超过4MB");
        }
        return new String(buf, 0, len, StandardCharsets.UTF_8);
    }
}
